package com.sunwayhorizo.health.platform.security.service.operation;

import java.util.List;

import javax.ejb.Stateless;

import org.jboss.seam.Component;
import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.dto.ResultsDTO;
import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.platform.base.BaseManager;
import com.sunwayhorizo.health.platform.security.dao.SecurityOperationDao;
import com.sunwayhorizo.health.platform.security.dao.SecurityPermissionDao;
import com.sunwayhorizo.health.platform.security.persistent.SecurityOperation;

@Name("SecurityOperationManager")
@Stateless
public class SecurityOperationManager extends BaseManager implements SecurityOperationManagerLocal{

	@Override
	public ResultsDTO newSecurityOperation(SecurityOperation operation) {
		SecurityOperationDao operationDao = (SecurityOperationDao)Component.getInstance("SecurityOperationDao");
		operationDao.newSecurityOperation(operation);
		return getResultsDTO("新增操作名称成功", true, operation.getOperationId());
	}

	@Override
	public ResultsDTO delSecurityOperation(List<String> list) {
		SecurityPermissionDao permissionDao = (SecurityPermissionDao)Component.getInstance("SecurityPermissionDao");
		if( ListUtil.isNotEmpty( list )){
			for(String operationIdVar : list ){
				if( permissionDao.findSecurityPermissionListSizeByOperationId(operationIdVar) > 0 ){
					return super.getResultsDTO("操作被其他菜单关联，请删除关联后再次请求", false, null);
				}
			}
		}
		SecurityOperationDao operationDao = (SecurityOperationDao)Component.getInstance("SecurityOperationDao");
		operationDao.deleteSecurityOperation(list);
		return super.getResultsDTO("删除按钮列表成功", true, null);
	}

	@Override
	public ResultsDTO updateSecurityOperation(SecurityOperation operation) {
		SecurityOperationDao operationDao = (SecurityOperationDao)Component.getInstance("SecurityOperationDao");
		operationDao.updateSecurityOperation(operation);
		return super.getResultsDTO("修改按钮成功", true, null);
	}

	@Override
	public ResultsDTO findSecurityOperationList() {
		List<SecurityOperation> list = ((SecurityOperationDao)Component.getInstance("SecurityOperationDao")).findSecurityOperationList();
		return super.getResultsDTO("查询操作列表成功",true, list);
	}

	@Override
	public ResultsDTO findSecurityOperationList(String operationName) {
		List<SecurityOperation> list = ((SecurityOperationDao)Component.getInstance("SecurityOperationDao")).findSecurityOperationList(operationName);
		return super.getResultsDTO("查询操作列表成功",true, list);
	}
	
	@Override
	public ResultsDTO findSecurityOperationDetailById(String operationId) {
		SecurityOperation securityOperation = ((SecurityOperationDao)Component.getInstance("SecurityOperationDao")).findSecurityOperationDetailById(operationId);
		return super.getResultsDTO("查询成功",true, securityOperation);
	}

	@Override
	public ResultsDTO validateUniqueSecurityOperation(String operationId, String operationName) {
		SecurityOperationDao operationDao = (SecurityOperationDao)Component.getInstance("SecurityOperationDao");
		try{
			SecurityOperation dbOperationByName = operationDao.findSecurityOperationDetailByName(operationName);
			if(StrUtil.isEmpty(operationId)){
				return super.getResultsDTO("操作名称已经存在",false, null);
			}else{
				if( operationId.equals(dbOperationByName.getOperationId())){
					return super.getResultsDTO("操作名称可以使用",true, null);
				}else{
					return super.getResultsDTO("操作名称已经存在",false, null);
				}
			}
		}catch(Exception e){
			return super.getResultsDTO("操作名称可以使用",true, null);
		}
	}
}
